package com.hln.jingwushi.ui

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.hjq.toast.ToastUtils
import com.hln.jingwushi.databinding.ActivityRtspBinding
import com.gdsz.libbasic.view.ui.BaseActivity
import org.videolan.libvlc.LibVLC
import org.videolan.libvlc.Media
import org.videolan.libvlc.MediaPlayer


class VideoPlayerActivity : BaseActivity<ActivityRtspBinding>() {
    lateinit var mediaPlayer : MediaPlayer
    lateinit var libVLC : LibVLC

    var rtspUrl = "rtsp://admin:hln12345678@192.168.1.166"

    companion object {
        fun toMe (rtspUrl : String,context: Activity) {
            if (rtspUrl.isNullOrEmpty()) {
                ToastUtils.show("地址不能为空！")
                return
            }
            context.startActivity(Intent(context,VideoPlayerActivity::class.java).putExtra  (   "rtspUrl",rtspUrl))
        }
    }

    override fun ActivityRtspBinding.initBinding(savedInstanceState: Bundle?) {
//        var libVLC = LibVLC(this@VideoPlayerActivity)

        rtspUrl = intent.getStringExtra("rtspUrl").toString()
        val options = ArrayList<String>()
        options.add("--no-audio")
         libVLC = LibVLC(this@VideoPlayerActivity, options)
        mediaPlayer = MediaPlayer(libVLC)
        mediaPlayer.setVideoTrackEnabled(true);


        // Create Media
        val media = Media(libVLC, Uri.parse(rtspUrl))
        mediaPlayer.media = media
        media.release()

        // Set video output to video layout

        // Set video output to video layout
        mediaPlayer.attachViews(binding.surfaceVideo, null, false, false)
        mediaPlayer.play()

    }

    override fun onDestroy() {
        super.onDestroy()
        mediaPlayer.release()
        libVLC.release()
    }

}
